package com.example.tool.activtiy;

import android.os.Build;
import android.support.annotation.RequiresApi;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.tool.R;
import com.example.tool.activtiy.base.BaseActivity;
import com.example.tool.mvp.CommonPresent;

import butterknife.BindView;

/**
 * Created Date 2019/8/30
 *
 * @author liangkun
 * description：流式布局
 */
public class InflateActivity extends BaseActivity<CommonPresent> {
    @BindView(R.id.tv_title)
    protected TextView tvTitle;
    @BindView(R.id.rl_container)
    protected LinearLayout relativeLayout;
    @BindView(R.id.pop_lay)
    protected FrameLayout pop_lay;
    @BindView(R.id.mask_view)
    protected View mask_view;
    @BindView(R.id.toolbarRightIcon)
    protected ImageView imageView;
    private View inflateView;

    @Override
    protected int getLayoutId() {
        return R.layout.init_layout_activity;
    }

    @Override
    protected void initView() {
        tvTitle.setText("Inflate原理");
        imageView.setImageResource(R.mipmap.icon_filter);
        getLayoutInflater().inflate(R.layout.inflater_item_one, relativeLayout, true);


        /*根节点的宽高有效，不将inflate添加到constraintLayout。。。。*/
        View inflate = getLayoutInflater().inflate(R.layout.inflater_item_two, relativeLayout, false);
        relativeLayout.addView(inflate);
        /*root为null表示将第一个参数所指定的布局添加到任何容器中，根节点的宽高属性会失效*/
        View inflate1 = getLayoutInflater().inflate(R.layout.inflater_item_three, null, false);
        relativeLayout.addView(inflate1);
    }

    @RequiresApi(api = Build.VERSION_CODES.N)
    @Override
    protected void initData() {

    }


    @Override
    protected void initEvent() {
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (inflateView == null) {
                    inflateView = getLayoutInflater().inflate(R.layout.inflater_item_four, pop_lay, false);
                    pop_lay.addView(inflateView);
                    inflateView.setAnimation(AnimationUtils.loadAnimation(InflateActivity.this, R.anim.dd_menu_in));
                    mask_view.setVisibility(View.VISIBLE);
                } else {
                    if (inflateView.getVisibility() == View.VISIBLE) {
                        closeInflate();
                    } else {
                        inflateView.setVisibility(View.VISIBLE);
                        inflateView.setAnimation(AnimationUtils.loadAnimation(InflateActivity.this, R.anim.dd_menu_in));
                        mask_view.setVisibility(View.VISIBLE);
                    }
                }

            }
        });
        mask_view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
             closeInflate();
            }
        });
    }

    private void closeInflate() {
        if (inflateView.getVisibility() == View.GONE) {
            return;
        }
        inflateView.setAnimation(AnimationUtils.loadAnimation(InflateActivity.this, R.anim.dd_menu_out));
        inflateView.setVisibility(View.GONE);
        mask_view.setVisibility(View.GONE);
    }


    @Override
    protected CommonPresent createPresenter() {
        return null;
    }


}
